package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

// TbAttachment ------------------------------------------------------邮件附件表---------------------------------------------------------------------------------
const (
	TbAttachment db.TbName = "attachments"
)

const (
	AttachmentMailId   db.TbCol = "mail_id"
	AttachmentItemId   db.TbCol = "item_id"
	AttachmentNum      db.TbCol = "num"
	AttachmentSourceId db.TbCol = "source_id"
)

type Attachment struct {
	migrate.OneModel
	MailId   uint64 `gorm:"comment:邮件id"`
	ItemId   uint64 `gorm:"comment:物品id"`
	Num      uint64 `gorm:"comment:数量"`
	SourceId uint64 `gorm:"comment:来源id"`
}

func NewAttachment(mailId uint64, itemId uint64, num uint64, sourceId uint64) *Attachment {
	ret := &Attachment{}
	ret.ID = snowflake.GetId()
	ret.SetMailId(mailId)
	ret.SetItemId(itemId)
	ret.SetNum(num)
	ret.SetSourceId(sourceId)
	ret.SetUpdateAt(time.Now())
	ret.SetCreateAt(time.Now())
	return ret
}
func (receiver *Attachment) SetCreateAt(val time.Time) {
	receiver.CreatedAt = val
	syndb.AddDataToLazyChan(TbAttachment, db.CreatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *Attachment) SetUpdateAt(val time.Time) {
	receiver.UpdatedAt = val
	syndb.AddDataToLazyChan(TbAttachment, db.UpdatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *Attachment) SetMailId(mailId uint64) {
	receiver.MailId = mailId
	syndb.AddDataToLazyChan(TbAttachment, AttachmentMailId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: mailId,
	})
}
func (receiver *Attachment) SetItemId(itemId uint64) {
	receiver.ItemId = itemId
	syndb.AddDataToLazyChan(TbAttachment, AttachmentItemId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: itemId,
	})
}
func (receiver *Attachment) SetNum(num uint64) {
	receiver.Num = num
	syndb.AddDataToLazyChan(TbAttachment, AttachmentNum, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: num,
	})
}
func (receiver *Attachment) SetSourceId(sourceId uint64) {
	receiver.SourceId = sourceId
	syndb.AddDataToLazyChan(TbAttachment, AttachmentSourceId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: sourceId,
	})
}

func initAttachment() {
	migrate.AutoMigrate(&Attachment{})
	syndb.RegLazyWithMiddle(TbAttachment, AttachmentMailId)
	syndb.RegLazyWithMiddle(TbAttachment, AttachmentItemId)
	syndb.RegLazyWithMiddle(TbAttachment, AttachmentNum)
	syndb.RegLazyWithMiddle(TbAttachment, AttachmentSourceId)
	syndb.RegLazyWithMiddle(TbAttachment, db.CreatedAtName)
	syndb.RegLazyWithMiddle(TbAttachment, db.UpdatedAtName)
}
